package photobox.manage

import grails.converters.JSON
import photobox.order.Order
import photobox.order.OrderItem
import photobox.utils.ParamUtils
import photobox.utils.ResultUtil

class MngOrderController {
def orderService
    def list(){
        if(params.orderId){
            def order = Order.get(params.orderId)
            switch (Integer.parseInt(params.state.toString())){
                case 40:
                    order.orderStatus=order.ORDER_STATUS_SEND
                    break;
                case 30:
                    order.orderStatus=order.ORDER_STATUS_MAKING
                    break;
            }
            order.save()
        }
        def searchParams = ParamUtils.parseFilter(params.filters)
        def pageParams = ParamUtils.parsePage(params)
        def sortParams = ParamUtils.parseSort(params,
                ["id", "totalPrice", "dateCreated","orderStatus"], [sort: 'id', order: "desc"])

        def orderRs = orderService.mngOrderList(searchParams, pageParams, sortParams)
        render ResultUtil.pageResults(orderRs.rs, orderRs.total, pageParams,
                [id: 'id', totalPrice: 'totalPrice', dateCreated: 'dateCreated',orderStatus:'orderStatus']) as JSON
    }
    def index() {
//        def order = Order.findAll()
//     [order:order]
    }
    def list_deliver(){
        def searchParams = ParamUtils.parseFilter(params.filters)
        def pageParams = ParamUtils.parsePage(params)
        def sortParams = ParamUtils.parseSort(params,
                ["id", "totalPrice", "dateCreated","orderStatus"], [sort: 'id', order: "desc"])

        def orderRs = orderService.list_deliver(searchParams, pageParams, sortParams)
        render ResultUtil.pageResults(orderRs.rs, orderRs.total, pageParams,
                [id: 'id', totalPrice: 'totalPrice', dateCreated: 'dateCreated',orderStatus:'orderStatus']) as JSON
    }

    def order_deliver(){

    }
    def detail(){
        def order = Order.findById(params.id)
        def orderItems = OrderItem.findAllById(params.id)

        [order:order,orderItems:orderItems]
    }
//    def changeStatus(){
//        def order = Order.findById(params.id)
//        if(order.orderStatus < 40){
//            order.orderStatus =order.ORDER_STATUS_SEND
//            order.save()
//        }
//    }
}
